package com.android.semita;

import java.util.List;
import com.android.semita.model.InvoiceItem;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class InvoiceItemAdapter extends ArrayAdapter<InvoiceItem> {
	int resource;
	
	public InvoiceItemAdapter(Context context, int resource, List<InvoiceItem> items) {
		super(context, resource, items);
		this.resource = resource;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LinearLayout newView;
		InvoiceItem classInstance = getItem(position);

		String productName = classInstance.getProductName();
		String productQuantity = Integer.toString(classInstance.getQuantity());
		String productAmount = Float.toString(classInstance.getNetItemAmount());

		// TODO Retrieve values to display from the classInstance variable.
		// Inflate a new view if this is not an update.
		if (convertView == null) {
			newView = new LinearLayout(getContext());
			String inflater = Context.LAYOUT_INFLATER_SERVICE;
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
					inflater);
			vi.inflate(resource, newView, true);
		} else {
			newView = (LinearLayout) convertView;
		}

		TextView tvProductName = (TextView) newView.findViewById(R.id.tvProductName);
		TextView tvProductQuantity = (TextView) newView.findViewById(R.id.tvQuantity);
		TextView tvProductAmount = (TextView) newView.findViewById(R.id.tvItemAmount);

		tvProductName.setText(productName);
		tvProductQuantity.setText(productQuantity);
		tvProductAmount.setText(productAmount);

		return newView;
	}
}
